<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑分类' : '新增分类'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="分类名">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入分类名' }] }]" placeholder="请输入分类名"/>
        </a-form-item>
        <a-form-item v-show="false" label="id">
          <a-input v-decorator="['id', { initialValue: model?.id }]" />
        </a-form-item>
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item label="级别" v-show="!model?.id" >
          <a-select v-decorator="['type', { initialValue: '1' }]" @select="change" placeholder="请选择">
            <a-select-option value="1">一级分类</a-select-option>
            <a-select-option value="2">二级分类</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-if="model?.type == '2' || this.type == '2' " label="上级分类">
          <a-select v-decorator="['parentId', { rules: [{ required: true, message: '请输入上级分类' }] }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in option" :key="i.id">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-if="model && model.id > 0" label="状态">
          <a-select v-decorator="['status', { initialValue: model?.status }]" placeholder="请选择">
            <a-select-option value="1">使用中</a-select-option>
            <a-select-option value="0">已停用</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getGdClassListUsingPOST } from '@/api/api/shangpinguanli.ts'

// 表单字段
const fields = ['name','parentId','type','status', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      type: null,
      form: this.$form.createForm(this),
      option: [],
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
        classType: 1,
      },
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      console.log(this.model)
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  mounted() {
    this.getOption()
  },
  methods: {
    change(e){
      if(e){
        this.type = e
      }else{
        this.type = null
      }
    },
    getOption() {
      getGdClassListUsingPOST(this.optionParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
